﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucMenu : System.Web.UI.UserControl
    {
        cmsMenuDO objMenu = new cmsMenuDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                bindToDropDown(ddlMenu);
          
            if (!string.IsNullOrEmpty(Request.QueryString["MenuID"]))
            {
                lblAction.Text = "Sửa menu";
                int menuID = int.Parse(Request.QueryString["MenuID"]);
                objMenu.MenuID = menuID;
                InitForm();
            }
            else
                lblAction.Text = "Thêm menu";
        }

        protected void InitObject()
        {
            objMenu = new cmsMenuBL().Select(objMenu);
            objMenu.Title=txtTitle.Text.Trim();
            objMenu.OrderID = int.Parse(txtOrder.Text.Trim());
            objMenu.Link = txtLinkMenu.Text; //txtLink.Text.Trim();
            objMenu.ParentID = int.Parse(ddlMenu.SelectedValue);
            objMenu.Type = int.Parse(ddlType.SelectedValue);
            objMenu.Module = int.Parse(ddlModule.SelectedValue);
            int parent = int.Parse(ddlMenu.SelectedValue);
            cmsMenuDO objMenux = new cmsMenuBL().Select(new cmsMenuDO(){MenuID = parent});
            if (parent == 0 || objMenux.ParentID == 0)
            {
                objMenux.RootLevel = 1;
            }
            else 
            {
                objMenu.RootLevel = 2;
            }

        }
        protected void InitForm()
        {
            objMenu = new cmsMenuBL().Select(objMenu);
            txtTitle.Text = objMenu.Title;
            txtOrder.Text = objMenu.OrderID.ToString();
            
            txtLinkMenu.Text = objMenu.Link;
            try
            {
                
                ddlModule.SelectedValue = objMenu.Module.ToString();
                ddlMenu.SelectedValue = objMenu.ParentID.ToString();
                ddlType.SelectedValue = objMenu.Type.ToString();
            }
            catch 
            {
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            InitObject();
            if (objMenu.MenuID > 0)
            {
                new cmsMenuBL().Update(objMenu);
            }
            else
            {
                new cmsMenuBL().Insert(objMenu);
            }
            Functions.Alert("Cập nhật thành công!", "Default.aspx?Page=ListMenu");
        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsMenuBL().SelectAll();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["MenuID"].ToString();
                    item.Text = "+ " + dr["Title"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["MenuID"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = "-------" + dr["Title"].ToString();
                    listChild.Value = dr["MenuID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
             //       GetChildItems(dr["MenuID"].ToString(), dtTemp, ddl);
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=ListMenu");
        }

    }
}